package com.lds.erp.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import org.springframework.util.ObjectUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

/**
 * @author hongsir
 * @apiNote 重写一个HttpServletRequest, 避免 getInputStream() request.getReader() 一次性失效的问题
 * 推荐:在post的地方使用
 * @date 2018/10/11 下午1:00
 */
public class RequestWrapper extends HttpServletRequestWrapper {

    private final String body;
    public static final ThreadLocal<String> TEMP = new InheritableThreadLocal<>();

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                // 失败
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }

        // {key:{业务参数}}
        String tempBody = stringBuilder.toString();
        // 原型json
        JSONObject jsonBody = JSON.parseObject(tempBody, Feature.OrderedField);
        JSONObject bizJsonData = jsonBody.getJSONObject("data");
        // 获取业务参数
        if (ObjectUtils.isEmpty(bizJsonData)) {
            body = tempBody;
            JSONObject tempJson = new JSONObject(true);
            tempJson.put("data", jsonBody.toJSONString());
            TEMP.set(tempJson.toJSONString());
        } else {
            body = bizJsonData.toString();
            TEMP.set(tempBody);
        }

    }

    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        ServletInputStream servletInputStream = new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }

            @Override
            public int read() {
                return byteArrayInputStream.read();
            }
        };
        return servletInputStream;

    }

    @Override
    public BufferedReader getReader() {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

    public String getBody() {
        return this.body;
    }
}
